package com.lichuang.fruitvarietysm.goods.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lichuang.fruitvarietysm.goods.entity.Goods;
import com.lichuang.fruitvarietysm.goods.entity.GoodsSkuAttr;
import com.lichuang.fruitvarietysm.goods.entity.Goodssku;
import com.lichuang.fruitvarietysm.goods.mapper.GoodsMapper;
import com.lichuang.fruitvarietysm.goods.mapper.GoodsskuMapper;
import com.lichuang.fruitvarietysm.goods.service.IGoodsService;
import com.lichuang.fruitvarietysm.goods.service.IGoodsskuService;
import com.lichuang.fruitvarietysm.goods.service.impl.GoodsSkuAttrServiceImpl;
import com.lichuang.fruitvarietysm.goods.vo.GoodsDetails;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author shuaihang
 * @since 2019-05-17
 */
@RestController
@RequestMapping("//goods")
public class GoodsController {

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    private IGoodsService goodsService;

    @Autowired
    private IGoodsskuService goodsskuService;

    @Autowired
    public GoodsSkuAttrServiceImpl goodsSkuAttrService;

    @Autowired
    public GoodsskuMapper goodsskuMapper;
    /*
    首页随机显示商品
     */
    @RequestMapping("/firstRandomCommodity")
    public List<Goods> firstRandomCommodity(@RequestParam("parent_id")Integer parent_id ){
        List<Goods> goodsList=goodsService.goodsList(parent_id);
        return goodsList;
    }

    /**
     * 显示三级标题的所有商品
     */
    @RequestMapping("/sParentThreeGoods")
    public Object sParentThreeGoods(com.baomidou.mybatisplus.extension.plugins.pagination.Page page,
                                         @RequestParam(value = "pid",defaultValue = "0")Integer pid,
                                         @RequestParam(value = "parent_id",defaultValue = "0")Integer parent_id,
                                         @RequestParam(value = "category_id",defaultValue = "0")Integer category_id,
                                         @RequestParam(value = "sort",required = false)String sort,
                                         @RequestParam(value = "start_price",required = false,defaultValue = "0") Integer start_price,
                                         @RequestParam(value = "end_price",required = false,defaultValue ="0") String end_price,
                                    @RequestParam(value = "attribute_id",required = false)String attribute_id){

        return  goodsService.sParentThreeGoods(page,attribute_id,pid,parent_id,category_id,sort,start_price,end_price);
    }


    @RequestMapping("/sGoodsDetailsNoSku")
    public Object sGoodsDetailsNoSku(@RequestParam("goods_id")Integer goods_id){
        int count=goodsskuService.sCountGoodSku(goods_id);
        GoodsDetails goodsDetails=new GoodsDetails();
        // id,库存、商品名、商品副标题、商品状态、商品价格、商品周销量、积分、重量
        Goods goods=goodsService.sGoodsDetailsNoSku(goods_id);
        goodsDetails.setGoodsId(goods.getGoodsId());
        goodsDetails.setGoodsName(goods.getGoodsName());
        goodsDetails.setGoodsSubtitle(goods.getGoodsSubtitle());
        goodsDetails.setGoodsPrice(goods.getGoodsPrice());
        goodsDetails.setGoodsStock(goods.getGoodsStock());
        goodsDetails.setGoodsStatus(goods.getGoodsStatus());
        goodsDetails.setGoodsTotalSales(goods.getGoodsTotalSales());
        goodsDetails.setGoodsWeeklySales(goods.getGoodsWeeklySales());
        goodsDetails.setWeight(goods.getWeight());
        if(count!=0) {
            //存在sku的话查sku的对应的属性
            List<GoodsSkuAttr> goodsSkuAttrs = goodsSkuAttrService.sSkuAttribute(goods_id, -1);
            goodsDetails.setGoodsSkuAttrs(goodsSkuAttrs);
        }
        return goodsDetails;
    }

    //根据商品id，查对应的sku表对应数据
    @RequestMapping("/sAttributeValue")
    public Object sAttributeValue(@RequestParam("goods_id")Integer goods_id, @RequestParam("attribute_value")String attribute_value){
        List<Goodssku> list= goodsskuService.sAttributeValue(goods_id,"%"+attribute_value+"%");
        List<GoodsSkuAttr> goodsSkuAttrList1=new ArrayList<>();
        List<GoodsSkuAttr> goodsSkuAttrList2=new ArrayList<>();
        List<GoodsSkuAttr> goodsSkuAttrList3=new ArrayList<>();
        for (Goodssku lists:list) {
            String s = lists.getAttributeValue();
            String[] split1 = s.split(":");
            int count = 0;
            for (String s1 : split1) {
                count++;
                if (count == 2) {
                    int id1 = Integer.parseInt(s1);
                    GoodsSkuAttr goodsSkuAttr = goodsskuService.sAttName(id1);
                    goodsSkuAttrList1.add(goodsSkuAttr);
                }else if(count==3){
                    int id2 = Integer.parseInt(s1);
                    GoodsSkuAttr goodsSkuAttr2 = goodsskuService.sAttName(id2);
                    goodsSkuAttrList2.add(goodsSkuAttr2);
                }else if(count==4){
                    int id3 = Integer.parseInt(s1);
                    GoodsSkuAttr goodsSkuAttr3 = goodsskuService.sAttName(id3);
                    goodsSkuAttrList3.add(goodsSkuAttr3);
                }
            }
        }
        List<List<GoodsSkuAttr>> goodsSkuAttrList=new ArrayList<>();
        goodsSkuAttrList.add(goodsSkuAttrList1);
        goodsSkuAttrList.add(goodsSkuAttrList2);
        goodsSkuAttrList.add(goodsSkuAttrList3);
        return goodsSkuAttrList;
    }

    //根据属性值来判断，用户选择的sku。然后当作参数传给前端，进行价格，重量的一些显示。
    //http://localhost:8080//getSku?goods_id=1&skuValue=2:7:10
    @RequestMapping("/getSku")
    public Goodssku getSku(@RequestParam("goods_id")Integer goods_id,@RequestParam("skuValue")String skuValue){
        String state=null;
        Goodssku goodssku1=new Goodssku();
        if(skuValue==null){
            state="没有sku的value的值";
        }else {
            Goodssku goodssku = new Goodssku();
            goodssku.setGoodsId(goods_id);
            goodssku.setAttributeValue(skuValue);
            QueryWrapper<Goodssku> wrapper = new QueryWrapper<>(goodssku);
            //根据传过来的sku的值，查对应的价格，库存。
            goodssku1 = goodsskuMapper.selectOne(wrapper);
        }
        return goodssku1;
    }


    //查商品的评价总数
    @RequestMapping("/goodsEvaluate")
    public int goodsEvaluate(@RequestParam("goods_id")Integer goods_id){
        int count=goodsService.goodEvaluate(goods_id);
        return  count;
    }


    /**
     * 根据商品id获取全部商品信息
     * @param goodsId
     * @return
     */
    @RequestMapping("/getGoods")
    public Goods getGoods(@RequestParam("goodsId")Integer goodsId){
//        Goods goods=new Goods();
//        goods.setGoodsId(goodsId);
//        QueryWrapper queryWrapper=new QueryWrapper(goods);
//        Goods getGood=goodsMapper.selectOne(queryWrapper);
//        return  getGood;
        return goodsMapper.selectById(goodsId);
    }

    @RequestMapping("/updateGoods")
    public void updateGoods(@RequestBody Goods goods){
        goodsMapper.updateById(goods);
    }

}
